﻿using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;

namespace MongoDBTest2
{
    class Program
    {
        static void Main(string[] args)
        {
            MongoClient client = new MongoClient("mongodb://127.0.0.1");

            IMongoDatabase db = client.GetDatabase("TestDb1");

            IMongoCollection<Person> persons = db.GetCollection<Person>("Persons");

            //persons.DeleteMany(p => true);

            ////插入测试数据
            //List<Person> list = new List<Person>()
            //{
            //    new Person(){ Age=5,Name="Tom",Height=80 },
            //    new Person(){ Age=6,Name="Jim",Height=90 },
            //    new Person(){ Age=7,Name="Lily",Height=100 },
            //    new Person(){ Age=8,Name="Lucy",Height=110 },
            //    new Person(){ Age=9,Name="Tim",Height=120 },
            //};

            //persons.InsertMany(list);
            //Console.WriteLine("插入成功!");


            //查询
            #region 同步Find
            //同步Find
            //var filter = Builders<Person>.Filter.Gt(p => p.Age, 5); //age>5
            //var filter = Builders<Person>.Filter.Gte(p => p.Age, 5); //age>=5
            //var filter = Builders<Person>.Filter.Ne(p => p.Age, 5); //age!=5

            //age>5 && height>80 这种写法太复杂,一般用where
            //var filter=Builders<Person>.Filter.And(
            //    Builders<Person>.Filter.Gt(p=>p.Age,5),
            //    Builders<Person>.Filter.Gt(p => p.Height, 80)
            //);

            //where
            //var filter = Builders<Person>.Filter.Where(p => p.Age > 5 && p.Height > 80);

            //var result = persons.Find(filter);

            //result.ToList();

            //foreach (var item in result.ToList())
            //{
            //    Console.WriteLine($"{item.Id},{item.Name},{item.Age},{item.Height}");
            //}
            #endregion



            #region 异步FindAsync
            //调用异步FindAsync
            //var filter = Builders<Person>.Filter.Where(p => p.Age > 5 && p.Height > 80);

            //using (IAsyncCursor<Person> results = persons.FindAsync(filter).Result)
            //{
            //    //MoveNextAsync:获取下一组数据
            //    while (results.MoveNextAsync().Result)
            //    {
            //        //Current:就是当前组的数据
            //        IEnumerable<Person> items=results.Current;
            //        foreach (Person person in items)
            //        {
            //            Console.WriteLine($"{person.Id},{person.Name},{person.Age},{person.Height}");
            //        }
            //    }

            //}


            //数据量不大直接ToList、ToListAsync
            //var filter = Builders<Person>.Filter.Where(p => p.Age > 5 && p.Height > 80);
            //var list=persons.FindAsync(filter).Result.ToList();
            //foreach (Person person in list)
            //{
            //    Console.WriteLine($"{person.Id},{person.Name},{person.Age},{person.Height}");
            //}

            #endregion


            #region Mongodb查询是区分大小写的
            //var filter = Builders<Person>.Filter.Where(p => p.Name == "tom"); //数据库里是Tom!=tom,查询不出来
            //var list = persons.Find(filter).ToList();
            //foreach (var item in list)
            //{
            //    Console.WriteLine(item.Name);
            //}
            #endregion


            #region 分页查询
            //var filter = Builders<Person>.Filter.Where(p => true);

            //var sort = Builders<Person>.Sort.Descending(p => p.Age).Ascending(p => p.Name);//指定排序

            //FindOptions<Person, Person> findOpt = new FindOptions<Person, Person>();
            //findOpt.Skip = 1;  //跳过一行
            //findOpt.Limit = 2; //取两行
            //findOpt.Sort = sort;


            //using (var cursor = persons.FindAsync<Person>(filter, findOpt).Result)
            //{
            //    var items = cursor.ToList();
            //    foreach (var person in items)
            //    {
            //        Console.WriteLine($"{person.Id},{person.Name},{person.Age},{person.Height}");
            //    }
            //}
            #endregion

            #region 取出BsonDocument
            //IMongoCollection<BsonDocument> personsBsonDoc = db.GetCollection<BsonDocument>("Persons");

            //var filter1=Builders<BsonDocument>.Filter.Gt("Age",5);

            //using (var personsCursor = personsBsonDoc.FindAsync(filter1).Result)
            //{
            //    foreach (var p in  personsCursor.ToListAsync().Result)
            //    {
            //        Console.WriteLine(p.GetValue("Name").AsString);
            //    }
            //}
            #endregion


            #region 更新
            //var filter = Builders<Person>.Filter.Where(p => p.Age > 7);
            //var update = Builders<Person>.Update.Set(p => p.Name, "Hello");
            //persons.UpdateMany(filter, update);
            #endregion


            #region 删除
            var filter=Builders<Person>.Filter.Where(p=>p.Age<7);
            persons.DeleteMany(filter);
            #endregion

            Console.WriteLine("操作成功");
            Console.ReadKey();
        }
    }
}
